﻿using System;
using TechTalk.SpecFlow;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;
using System.IO;
using System.Threading;

namespace Sketch.Test.Acceptance
{
    [Binding]
    public class GenericSteps : Steps
    {
        public static GenericSteps Instance = new GenericSteps();

        [Then(@"I should be on the page (.*)")]
        public void ThenIShouldBeOnTheHomeGuestbookPage(string page)
        {
            Assert.IsTrue(WebBrowser.Current.Url.EndsWith(page));
        }

        [When(@"I enter ""(.*)"" as ""(.*)""")]
        [Given(@"I enter ""(.*)"" as ""(.*)""")]
        public void EnterTextToControlLabeledAs(string text, string label)
        {
            var control = WebBrowser.Current.TextFields.First(Find.ByLabelText(label));
            Assert.IsNotNull(control, string.Format("Text control with label '{0}' not found", label));
            control.Clear();
            control.AppendText(text);
        }

        [When(@"I select ""(.*)"" as ""(.*)""")]
        [Given(@"I select ""(.*)"" as ""(.*)""")]
        public void SelectValueInControlLabeledAs(string text, string label)
        {
            WebBrowser.Current.SelectLists.First(Find.ByLabelText(label)).Select(text);
        }

        [When(@"I navigate to the (.*)")]
        [Given(@"I navigate to the (.*)")]
        [Given(@"I am on the page (.*)")]
        public void NavigateTo(Uri url)
        {
            WebBrowser.Current.GoTo(url);            
        }

        [Given(@"I am on the sketch page")]
        public void NavigateToSketchPage()
        {
            Given("I navigate to the /index.html");
        }

        [When(@"I press the button labeled ""(.*)""")]
        public void PressTheButtonLabeled(string label)
        {
            var button = WebBrowser.Current.Buttons.First(Find.ByElement(p=>p.InnerHtml.Contains(label) || p.GetAttributeValue("value") == label));
            button.Click();
        }

        [When(@"I click on link labeled ""(.*)""")]
        public void ClickOnLinkLabeled(string label)
        {
            WebBrowser.Current.Links.First(Find.ByText(x => x.Equals(label, StringComparison.OrdinalIgnoreCase))).Click();
        }

        [When(@"I choose ""(.*)"" in drop down labeled as ""(.*)""")]
        public void ChooseInDropDownLabeledAs(string valueLabel, string fieldLabel)
        {
            WebBrowser.Current.SelectLists.First(Find.ByLabelText(fieldLabel)).Select(valueLabel);
        }

        [When(@"I select check box labeled as ""(.*)""")]
        public void MarkCheckBox(string fieldLabel)
        {
            WebBrowser.Current.CheckBoxes.First(Find.ByLabelText(fieldLabel)).Checked = true;
        }

        [When(@"I select check box with id ""(.*)""")]
        public void MarkCheckBoxById(string id)
        {
            WebBrowser.Current.CheckBoxes.First(Find.ById(id)).Checked = true;
        }        

        [When(@"I select file ""(.*)"" as ""(.*)""")]
        public void UploadFile(string name, string labeled)
        {
            var file = WebBrowser.Current.FileUpload(Find.ByLabelText(labeled));
            file.Set(Path.Combine(Path.GetTempPath(), name));            
        }

        [When(@"I wait (.*)")]
        public void UploadFile(string time)
        {
            Thread.Sleep(int.Parse(time));
        }
    }
}
